
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 05/20/2011 13:20:31
-- Generated from EDMX file: C:\01DATA\Pratik_Data\Work\_VS2010_Projects\_CodePlex\BooksOnline\BooksOnlineModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [BooksOnline];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_BookDistributor_Book]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BookDistributor] DROP CONSTRAINT [FK_BookDistributor_Book];
GO
IF OBJECT_ID(N'[dbo].[FK_BookDistributor_Distributor]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BookDistributor] DROP CONSTRAINT [FK_BookDistributor_Distributor];
GO
IF OBJECT_ID(N'[dbo].[FK_BookReference]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[References] DROP CONSTRAINT [FK_BookReference];
GO
IF OBJECT_ID(N'[dbo].[FK_UserUserRole]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRoles] DROP CONSTRAINT [FK_UserUserRole];
GO
IF OBJECT_ID(N'[dbo].[FK_RoleUserRole]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRoles] DROP CONSTRAINT [FK_RoleUserRole];
GO
IF OBJECT_ID(N'[dbo].[FK_UserUserFavouriteBooks]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserFavouriteBooks] DROP CONSTRAINT [FK_UserUserFavouriteBooks];
GO
IF OBJECT_ID(N'[dbo].[FK_BookUserFavouriteBooks]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserFavouriteBooks] DROP CONSTRAINT [FK_BookUserFavouriteBooks];
GO
IF OBJECT_ID(N'[dbo].[FK_ReferenceUserFavouriteReferences]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserFavouriteReferences] DROP CONSTRAINT [FK_ReferenceUserFavouriteReferences];
GO
IF OBJECT_ID(N'[dbo].[FK_UserUserFavouriteReferences]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserFavouriteReferences] DROP CONSTRAINT [FK_UserUserFavouriteReferences];
GO
IF OBJECT_ID(N'[dbo].[FK_DistributorUserFavouriteDistributors]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserFavouriteDistributors] DROP CONSTRAINT [FK_DistributorUserFavouriteDistributors];
GO
IF OBJECT_ID(N'[dbo].[FK_UserUserFavouriteDistributors]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserFavouriteDistributors] DROP CONSTRAINT [FK_UserUserFavouriteDistributors];
GO
IF OBJECT_ID(N'[dbo].[FK_BookRecommendation]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Recommendations] DROP CONSTRAINT [FK_BookRecommendation];
GO
IF OBJECT_ID(N'[dbo].[FK_UserCustomers]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Customers] DROP CONSTRAINT [FK_UserCustomers];
GO
IF OBJECT_ID(N'[dbo].[FK_CustomersRecommendation]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Recommendations] DROP CONSTRAINT [FK_CustomersRecommendation];
GO
IF OBJECT_ID(N'[dbo].[FK_CustomersPurchases]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Purchases] DROP CONSTRAINT [FK_CustomersPurchases];
GO
IF OBJECT_ID(N'[dbo].[FK_PurchaseBook_Purchase]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PurchaseBook] DROP CONSTRAINT [FK_PurchaseBook_Purchase];
GO
IF OBJECT_ID(N'[dbo].[FK_PurchaseBook_Book]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PurchaseBook] DROP CONSTRAINT [FK_PurchaseBook_Book];
GO
IF OBJECT_ID(N'[dbo].[FK_PurchaseDistributor_Purchase]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PurchaseDistributor] DROP CONSTRAINT [FK_PurchaseDistributor_Purchase];
GO
IF OBJECT_ID(N'[dbo].[FK_PurchaseDistributor_Distributor]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PurchaseDistributor] DROP CONSTRAINT [FK_PurchaseDistributor_Distributor];
GO
IF OBJECT_ID(N'[dbo].[FK_RoleRole]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Roles] DROP CONSTRAINT [FK_RoleRole];
GO
IF OBJECT_ID(N'[dbo].[FK_ReviewBook]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Reviews] DROP CONSTRAINT [FK_ReviewBook];
GO
IF OBJECT_ID(N'[dbo].[FK_DistributorReview]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Reviews] DROP CONSTRAINT [FK_DistributorReview];
GO
IF OBJECT_ID(N'[dbo].[FK_ReferenceReview]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Reviews] DROP CONSTRAINT [FK_ReferenceReview];
GO
IF OBJECT_ID(N'[dbo].[FK_ReviewRating]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Reviews] DROP CONSTRAINT [FK_ReviewRating];
GO
IF OBJECT_ID(N'[dbo].[FK_BookDiscussion]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Discussions] DROP CONSTRAINT [FK_BookDiscussion];
GO
IF OBJECT_ID(N'[dbo].[FK_UserDiscussion]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Discussions] DROP CONSTRAINT [FK_UserDiscussion];
GO
IF OBJECT_ID(N'[dbo].[FK_UserDiscussion1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Discussions] DROP CONSTRAINT [FK_UserDiscussion1];
GO
IF OBJECT_ID(N'[dbo].[FK_DiscussionDiscussion]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Discussions] DROP CONSTRAINT [FK_DiscussionDiscussion];
GO
IF OBJECT_ID(N'[dbo].[FK_DistributorDiscussion]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Discussions] DROP CONSTRAINT [FK_DistributorDiscussion];
GO
IF OBJECT_ID(N'[dbo].[FK_ReferenceDiscussion]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Discussions] DROP CONSTRAINT [FK_ReferenceDiscussion];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Books]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Books];
GO
IF OBJECT_ID(N'[dbo].[Distributors]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Distributors];
GO
IF OBJECT_ID(N'[dbo].[References]', 'U') IS NOT NULL
    DROP TABLE [dbo].[References];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[Roles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Roles];
GO
IF OBJECT_ID(N'[dbo].[UserRoles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserRoles];
GO
IF OBJECT_ID(N'[dbo].[UserFavouriteBooks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserFavouriteBooks];
GO
IF OBJECT_ID(N'[dbo].[UserFavouriteReferences]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserFavouriteReferences];
GO
IF OBJECT_ID(N'[dbo].[UserFavouriteDistributors]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserFavouriteDistributors];
GO
IF OBJECT_ID(N'[dbo].[Recommendations]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Recommendations];
GO
IF OBJECT_ID(N'[dbo].[Customers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Customers];
GO
IF OBJECT_ID(N'[dbo].[Purchases]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Purchases];
GO
IF OBJECT_ID(N'[dbo].[Audits]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Audits];
GO
IF OBJECT_ID(N'[dbo].[Reviews]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Reviews];
GO
IF OBJECT_ID(N'[dbo].[Ratings]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Ratings];
GO
IF OBJECT_ID(N'[dbo].[RatingMetaMappings]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RatingMetaMappings];
GO
IF OBJECT_ID(N'[dbo].[Discussions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Discussions];
GO
IF OBJECT_ID(N'[dbo].[BookDistributor]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BookDistributor];
GO
IF OBJECT_ID(N'[dbo].[PurchaseBook]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PurchaseBook];
GO
IF OBJECT_ID(N'[dbo].[PurchaseDistributor]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PurchaseDistributor];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Books'
CREATE TABLE [dbo].[Books] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Author] nvarchar(max)  NULL,
    [ISBN] nvarchar(max)  NULL,
    [Price] nvarchar(max)  NULL
);
GO

-- Creating table 'Distributors'
CREATE TABLE [dbo].[Distributors] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [RegistrationId] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NULL,
    [Address] nvarchar(max)  NULL
);
GO

-- Creating table 'References'
CREATE TABLE [dbo].[References] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Type] nvarchar(max)  NULL,
    [IsCached] bit  NULL,
    [Book_Id] int  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [CreatedDate] datetime  NULL,
    [LastLogin] datetime  NULL,
    [IsEnabled] bit  NULL,
    [Password] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Roles'
CREATE TABLE [dbo].[Roles] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [IsAdmin] bit  NOT NULL,
    [RoleRole_ParentRole_Id] int  NULL
);
GO

-- Creating table 'UserRoles'
CREATE TABLE [dbo].[UserRoles] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [User_Id] int  NOT NULL,
    [Role_Id] int  NOT NULL
);
GO

-- Creating table 'UserFavouriteBooks'
CREATE TABLE [dbo].[UserFavouriteBooks] (
    [Id] int  NOT NULL,
    [User_Id] int  NOT NULL,
    [Book_Id] int  NOT NULL
);
GO

-- Creating table 'UserFavouriteReferences'
CREATE TABLE [dbo].[UserFavouriteReferences] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Reference_Id] int  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'UserFavouriteDistributors'
CREATE TABLE [dbo].[UserFavouriteDistributors] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Distributor_Id] int  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'Recommendations'
CREATE TABLE [dbo].[Recommendations] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Text] nvarchar(max)  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [Book_Id] int  NOT NULL,
    [Customer_Id] int  NOT NULL
);
GO

-- Creating table 'Customers'
CREATE TABLE [dbo].[Customers] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'Purchases'
CREATE TABLE [dbo].[Purchases] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [PurchaseDate] datetime  NOT NULL,
    [Customer_Id] int  NOT NULL
);
GO

-- Creating table 'Audits'
CREATE TABLE [dbo].[Audits] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ObjectType] nvarchar(max)  NOT NULL,
    [ObjectName] nvarchar(max)  NOT NULL,
    [OldValue] nvarchar(max)  NOT NULL,
    [NewValue] nvarchar(max)  NOT NULL,
    [LastModifiedBy] nvarchar(max)  NOT NULL,
    [LastModifiedDate] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Reviews'
CREATE TABLE [dbo].[Reviews] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Text] nvarchar(max)  NOT NULL,
    [CreatedDate] nvarchar(max)  NOT NULL,
    [CreatedBy] nvarchar(max)  NOT NULL,
    [TargetType] nvarchar(max)  NOT NULL,
    [Book_Id] int  NULL,
    [Distributor_Id] int  NULL,
    [Reference_Id] int  NULL,
    [Rating_Id] int  NOT NULL
);
GO

-- Creating table 'Ratings'
CREATE TABLE [dbo].[Ratings] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [MetaProperty1] nvarchar(max)  NOT NULL,
    [MetaProperty2] nvarchar(max)  NOT NULL,
    [MetaProperty3] nvarchar(max)  NOT NULL,
    [MetaProperty4] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'RatingMetaMappings'
CREATE TABLE [dbo].[RatingMetaMappings] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [MetaPropertyName] nvarchar(max)  NOT NULL,
    [MetaPropertyValue] nvarchar(max)  NOT NULL,
    [EntityType] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Discussions'
CREATE TABLE [dbo].[Discussions] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Subject] nvarchar(max)  NOT NULL,
    [CreatedDate] nvarchar(max)  NOT NULL,
    [LastPostDate] nvarchar(max)  NOT NULL,
    [IsReply] bit  NOT NULL,
    [Book_Id] int  NULL,
    [CreatedBy_Id] int  NOT NULL,
    [LastUpdatedBy_Id] int  NOT NULL,
    [ParentId_Id] int  NOT NULL,
    [Distributor_Id] int  NULL,
    [Reference_Id] int  NULL
);
GO

-- Creating table 'BookDistributor'
CREATE TABLE [dbo].[BookDistributor] (
    [Books_Id] int  NOT NULL,
    [Distributors_Id] int  NOT NULL
);
GO

-- Creating table 'PurchaseBook'
CREATE TABLE [dbo].[PurchaseBook] (
    [Purchases_Id] int  NOT NULL,
    [Books_Id] int  NOT NULL
);
GO

-- Creating table 'PurchaseDistributor'
CREATE TABLE [dbo].[PurchaseDistributor] (
    [Purchases_Id] int  NOT NULL,
    [Distributors_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Books'
ALTER TABLE [dbo].[Books]
ADD CONSTRAINT [PK_Books]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Distributors'
ALTER TABLE [dbo].[Distributors]
ADD CONSTRAINT [PK_Distributors]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'References'
ALTER TABLE [dbo].[References]
ADD CONSTRAINT [PK_References]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Roles'
ALTER TABLE [dbo].[Roles]
ADD CONSTRAINT [PK_Roles]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UserRoles'
ALTER TABLE [dbo].[UserRoles]
ADD CONSTRAINT [PK_UserRoles]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UserFavouriteBooks'
ALTER TABLE [dbo].[UserFavouriteBooks]
ADD CONSTRAINT [PK_UserFavouriteBooks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UserFavouriteReferences'
ALTER TABLE [dbo].[UserFavouriteReferences]
ADD CONSTRAINT [PK_UserFavouriteReferences]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UserFavouriteDistributors'
ALTER TABLE [dbo].[UserFavouriteDistributors]
ADD CONSTRAINT [PK_UserFavouriteDistributors]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Recommendations'
ALTER TABLE [dbo].[Recommendations]
ADD CONSTRAINT [PK_Recommendations]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Customers'
ALTER TABLE [dbo].[Customers]
ADD CONSTRAINT [PK_Customers]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Purchases'
ALTER TABLE [dbo].[Purchases]
ADD CONSTRAINT [PK_Purchases]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Audits'
ALTER TABLE [dbo].[Audits]
ADD CONSTRAINT [PK_Audits]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Reviews'
ALTER TABLE [dbo].[Reviews]
ADD CONSTRAINT [PK_Reviews]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Ratings'
ALTER TABLE [dbo].[Ratings]
ADD CONSTRAINT [PK_Ratings]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'RatingMetaMappings'
ALTER TABLE [dbo].[RatingMetaMappings]
ADD CONSTRAINT [PK_RatingMetaMappings]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Discussions'
ALTER TABLE [dbo].[Discussions]
ADD CONSTRAINT [PK_Discussions]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Books_Id], [Distributors_Id] in table 'BookDistributor'
ALTER TABLE [dbo].[BookDistributor]
ADD CONSTRAINT [PK_BookDistributor]
    PRIMARY KEY NONCLUSTERED ([Books_Id], [Distributors_Id] ASC);
GO

-- Creating primary key on [Purchases_Id], [Books_Id] in table 'PurchaseBook'
ALTER TABLE [dbo].[PurchaseBook]
ADD CONSTRAINT [PK_PurchaseBook]
    PRIMARY KEY NONCLUSTERED ([Purchases_Id], [Books_Id] ASC);
GO

-- Creating primary key on [Purchases_Id], [Distributors_Id] in table 'PurchaseDistributor'
ALTER TABLE [dbo].[PurchaseDistributor]
ADD CONSTRAINT [PK_PurchaseDistributor]
    PRIMARY KEY NONCLUSTERED ([Purchases_Id], [Distributors_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Books_Id] in table 'BookDistributor'
ALTER TABLE [dbo].[BookDistributor]
ADD CONSTRAINT [FK_BookDistributor_Book]
    FOREIGN KEY ([Books_Id])
    REFERENCES [dbo].[Books]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Distributors_Id] in table 'BookDistributor'
ALTER TABLE [dbo].[BookDistributor]
ADD CONSTRAINT [FK_BookDistributor_Distributor]
    FOREIGN KEY ([Distributors_Id])
    REFERENCES [dbo].[Distributors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BookDistributor_Distributor'
CREATE INDEX [IX_FK_BookDistributor_Distributor]
ON [dbo].[BookDistributor]
    ([Distributors_Id]);
GO

-- Creating foreign key on [Book_Id] in table 'References'
ALTER TABLE [dbo].[References]
ADD CONSTRAINT [FK_BookReference]
    FOREIGN KEY ([Book_Id])
    REFERENCES [dbo].[Books]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BookReference'
CREATE INDEX [IX_FK_BookReference]
ON [dbo].[References]
    ([Book_Id]);
GO

-- Creating foreign key on [User_Id] in table 'UserRoles'
ALTER TABLE [dbo].[UserRoles]
ADD CONSTRAINT [FK_UserUserRole]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserUserRole'
CREATE INDEX [IX_FK_UserUserRole]
ON [dbo].[UserRoles]
    ([User_Id]);
GO

-- Creating foreign key on [Role_Id] in table 'UserRoles'
ALTER TABLE [dbo].[UserRoles]
ADD CONSTRAINT [FK_RoleUserRole]
    FOREIGN KEY ([Role_Id])
    REFERENCES [dbo].[Roles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RoleUserRole'
CREATE INDEX [IX_FK_RoleUserRole]
ON [dbo].[UserRoles]
    ([Role_Id]);
GO

-- Creating foreign key on [User_Id] in table 'UserFavouriteBooks'
ALTER TABLE [dbo].[UserFavouriteBooks]
ADD CONSTRAINT [FK_UserUserFavouriteBooks]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserUserFavouriteBooks'
CREATE INDEX [IX_FK_UserUserFavouriteBooks]
ON [dbo].[UserFavouriteBooks]
    ([User_Id]);
GO

-- Creating foreign key on [Book_Id] in table 'UserFavouriteBooks'
ALTER TABLE [dbo].[UserFavouriteBooks]
ADD CONSTRAINT [FK_BookUserFavouriteBooks]
    FOREIGN KEY ([Book_Id])
    REFERENCES [dbo].[Books]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BookUserFavouriteBooks'
CREATE INDEX [IX_FK_BookUserFavouriteBooks]
ON [dbo].[UserFavouriteBooks]
    ([Book_Id]);
GO

-- Creating foreign key on [Reference_Id] in table 'UserFavouriteReferences'
ALTER TABLE [dbo].[UserFavouriteReferences]
ADD CONSTRAINT [FK_ReferenceUserFavouriteReferences]
    FOREIGN KEY ([Reference_Id])
    REFERENCES [dbo].[References]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReferenceUserFavouriteReferences'
CREATE INDEX [IX_FK_ReferenceUserFavouriteReferences]
ON [dbo].[UserFavouriteReferences]
    ([Reference_Id]);
GO

-- Creating foreign key on [User_Id] in table 'UserFavouriteReferences'
ALTER TABLE [dbo].[UserFavouriteReferences]
ADD CONSTRAINT [FK_UserUserFavouriteReferences]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserUserFavouriteReferences'
CREATE INDEX [IX_FK_UserUserFavouriteReferences]
ON [dbo].[UserFavouriteReferences]
    ([User_Id]);
GO

-- Creating foreign key on [Distributor_Id] in table 'UserFavouriteDistributors'
ALTER TABLE [dbo].[UserFavouriteDistributors]
ADD CONSTRAINT [FK_DistributorUserFavouriteDistributors]
    FOREIGN KEY ([Distributor_Id])
    REFERENCES [dbo].[Distributors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DistributorUserFavouriteDistributors'
CREATE INDEX [IX_FK_DistributorUserFavouriteDistributors]
ON [dbo].[UserFavouriteDistributors]
    ([Distributor_Id]);
GO

-- Creating foreign key on [User_Id] in table 'UserFavouriteDistributors'
ALTER TABLE [dbo].[UserFavouriteDistributors]
ADD CONSTRAINT [FK_UserUserFavouriteDistributors]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserUserFavouriteDistributors'
CREATE INDEX [IX_FK_UserUserFavouriteDistributors]
ON [dbo].[UserFavouriteDistributors]
    ([User_Id]);
GO

-- Creating foreign key on [Book_Id] in table 'Recommendations'
ALTER TABLE [dbo].[Recommendations]
ADD CONSTRAINT [FK_BookRecommendation]
    FOREIGN KEY ([Book_Id])
    REFERENCES [dbo].[Books]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BookRecommendation'
CREATE INDEX [IX_FK_BookRecommendation]
ON [dbo].[Recommendations]
    ([Book_Id]);
GO

-- Creating foreign key on [User_Id] in table 'Customers'
ALTER TABLE [dbo].[Customers]
ADD CONSTRAINT [FK_UserCustomers]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserCustomers'
CREATE INDEX [IX_FK_UserCustomers]
ON [dbo].[Customers]
    ([User_Id]);
GO

-- Creating foreign key on [Customer_Id] in table 'Recommendations'
ALTER TABLE [dbo].[Recommendations]
ADD CONSTRAINT [FK_CustomersRecommendation]
    FOREIGN KEY ([Customer_Id])
    REFERENCES [dbo].[Customers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CustomersRecommendation'
CREATE INDEX [IX_FK_CustomersRecommendation]
ON [dbo].[Recommendations]
    ([Customer_Id]);
GO

-- Creating foreign key on [Customer_Id] in table 'Purchases'
ALTER TABLE [dbo].[Purchases]
ADD CONSTRAINT [FK_CustomersPurchases]
    FOREIGN KEY ([Customer_Id])
    REFERENCES [dbo].[Customers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CustomersPurchases'
CREATE INDEX [IX_FK_CustomersPurchases]
ON [dbo].[Purchases]
    ([Customer_Id]);
GO

-- Creating foreign key on [Purchases_Id] in table 'PurchaseBook'
ALTER TABLE [dbo].[PurchaseBook]
ADD CONSTRAINT [FK_PurchaseBook_Purchase]
    FOREIGN KEY ([Purchases_Id])
    REFERENCES [dbo].[Purchases]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Books_Id] in table 'PurchaseBook'
ALTER TABLE [dbo].[PurchaseBook]
ADD CONSTRAINT [FK_PurchaseBook_Book]
    FOREIGN KEY ([Books_Id])
    REFERENCES [dbo].[Books]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PurchaseBook_Book'
CREATE INDEX [IX_FK_PurchaseBook_Book]
ON [dbo].[PurchaseBook]
    ([Books_Id]);
GO

-- Creating foreign key on [Purchases_Id] in table 'PurchaseDistributor'
ALTER TABLE [dbo].[PurchaseDistributor]
ADD CONSTRAINT [FK_PurchaseDistributor_Purchase]
    FOREIGN KEY ([Purchases_Id])
    REFERENCES [dbo].[Purchases]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Distributors_Id] in table 'PurchaseDistributor'
ALTER TABLE [dbo].[PurchaseDistributor]
ADD CONSTRAINT [FK_PurchaseDistributor_Distributor]
    FOREIGN KEY ([Distributors_Id])
    REFERENCES [dbo].[Distributors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PurchaseDistributor_Distributor'
CREATE INDEX [IX_FK_PurchaseDistributor_Distributor]
ON [dbo].[PurchaseDistributor]
    ([Distributors_Id]);
GO

-- Creating foreign key on [RoleRole_ParentRole_Id] in table 'Roles'
ALTER TABLE [dbo].[Roles]
ADD CONSTRAINT [FK_RoleRole]
    FOREIGN KEY ([RoleRole_ParentRole_Id])
    REFERENCES [dbo].[Roles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RoleRole'
CREATE INDEX [IX_FK_RoleRole]
ON [dbo].[Roles]
    ([RoleRole_ParentRole_Id]);
GO

-- Creating foreign key on [Book_Id] in table 'Reviews'
ALTER TABLE [dbo].[Reviews]
ADD CONSTRAINT [FK_ReviewBook]
    FOREIGN KEY ([Book_Id])
    REFERENCES [dbo].[Books]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReviewBook'
CREATE INDEX [IX_FK_ReviewBook]
ON [dbo].[Reviews]
    ([Book_Id]);
GO

-- Creating foreign key on [Distributor_Id] in table 'Reviews'
ALTER TABLE [dbo].[Reviews]
ADD CONSTRAINT [FK_DistributorReview]
    FOREIGN KEY ([Distributor_Id])
    REFERENCES [dbo].[Distributors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DistributorReview'
CREATE INDEX [IX_FK_DistributorReview]
ON [dbo].[Reviews]
    ([Distributor_Id]);
GO

-- Creating foreign key on [Reference_Id] in table 'Reviews'
ALTER TABLE [dbo].[Reviews]
ADD CONSTRAINT [FK_ReferenceReview]
    FOREIGN KEY ([Reference_Id])
    REFERENCES [dbo].[References]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReferenceReview'
CREATE INDEX [IX_FK_ReferenceReview]
ON [dbo].[Reviews]
    ([Reference_Id]);
GO

-- Creating foreign key on [Rating_Id] in table 'Reviews'
ALTER TABLE [dbo].[Reviews]
ADD CONSTRAINT [FK_ReviewRating]
    FOREIGN KEY ([Rating_Id])
    REFERENCES [dbo].[Ratings]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReviewRating'
CREATE INDEX [IX_FK_ReviewRating]
ON [dbo].[Reviews]
    ([Rating_Id]);
GO

-- Creating foreign key on [Book_Id] in table 'Discussions'
ALTER TABLE [dbo].[Discussions]
ADD CONSTRAINT [FK_BookDiscussion]
    FOREIGN KEY ([Book_Id])
    REFERENCES [dbo].[Books]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BookDiscussion'
CREATE INDEX [IX_FK_BookDiscussion]
ON [dbo].[Discussions]
    ([Book_Id]);
GO

-- Creating foreign key on [CreatedBy_Id] in table 'Discussions'
ALTER TABLE [dbo].[Discussions]
ADD CONSTRAINT [FK_UserDiscussion]
    FOREIGN KEY ([CreatedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserDiscussion'
CREATE INDEX [IX_FK_UserDiscussion]
ON [dbo].[Discussions]
    ([CreatedBy_Id]);
GO

-- Creating foreign key on [LastUpdatedBy_Id] in table 'Discussions'
ALTER TABLE [dbo].[Discussions]
ADD CONSTRAINT [FK_UserDiscussion1]
    FOREIGN KEY ([LastUpdatedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserDiscussion1'
CREATE INDEX [IX_FK_UserDiscussion1]
ON [dbo].[Discussions]
    ([LastUpdatedBy_Id]);
GO

-- Creating foreign key on [ParentId_Id] in table 'Discussions'
ALTER TABLE [dbo].[Discussions]
ADD CONSTRAINT [FK_DiscussionDiscussion]
    FOREIGN KEY ([ParentId_Id])
    REFERENCES [dbo].[Discussions]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DiscussionDiscussion'
CREATE INDEX [IX_FK_DiscussionDiscussion]
ON [dbo].[Discussions]
    ([ParentId_Id]);
GO

-- Creating foreign key on [Distributor_Id] in table 'Discussions'
ALTER TABLE [dbo].[Discussions]
ADD CONSTRAINT [FK_DistributorDiscussion]
    FOREIGN KEY ([Distributor_Id])
    REFERENCES [dbo].[Distributors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DistributorDiscussion'
CREATE INDEX [IX_FK_DistributorDiscussion]
ON [dbo].[Discussions]
    ([Distributor_Id]);
GO

-- Creating foreign key on [Reference_Id] in table 'Discussions'
ALTER TABLE [dbo].[Discussions]
ADD CONSTRAINT [FK_ReferenceDiscussion]
    FOREIGN KEY ([Reference_Id])
    REFERENCES [dbo].[References]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReferenceDiscussion'
CREATE INDEX [IX_FK_ReferenceDiscussion]
ON [dbo].[Discussions]
    ([Reference_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------